"use strict";

import {
    //核心
    React, Enhance, Controller, Component, $, EventEmitter,
    //
    tools, classnames,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid,
    BoxWidget, Input, Button, Prompt, Link, Loaders, Editor, Resource, Directive, Lock, Fix
} from 'admin';

import FroalaEditor from 'FroalaEditor';
import debug from 'debug';
import valid from '../validate/admin.json';

import Fittingtype from '../widget/w-fittingtype';


var { http, date, time } = tools;
const log = debug('ysw.fittingtype');

/**
 *仓库信息
 */
class FittingtypeController extends Controller {
    state = {
        table: {
            update: {},
            query: {},
            types: {
            },
            onRowClick: this._handlerRowClick,
            buttons: {
                'edit': {
                    onClick: this._handlerEditDialog
                }
            },
            pk: 'id',
            fields: [
                {
                    title: "#",
                    width: 1,
                    type: {
                        name: "#"
                    }
                },
                {
                    order: -1,
                    name: 'id',
                    width: 1,
                    display: false
                },
                {
                    order: 0,
                    name: 'code',

                },
                {
                    name: 'name',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'time',
                    type: {
                        name: 'datetime'
                    }
                },
                {
                    order: 0,
                    name: 'u_time',
                    type: {
                        name: 'datetime'
                    }
                },
            ]
        },
        row: {},
        edit: {}
    }

    /**
     * 在componentWillMount()在挂载发生之前立即被调用。
     */
    componentWillMount() {

    }

    /**
     *
     */
    constructor() {
        super();
    }

    render() {
        return (
            <Grid>
                <Grid col="12 12 12 12">
                    {this.renderTable()}
                </Grid>
                <Fittingtype ref="fittingtype" onCache={() => { this.forceUpdate() }} onAddSuccess={this._handlerAddSuccess} onEditSuccess={this._handlerEditSuccess}></Fittingtype>
            </Grid>
        );
    }

    /**
     *
     * @return {*}
     */
    renderTable() {
        return (
            <BoxWidget
                color="3"
                className="table-height scroll"
            >
                <div data-fix="buttons">
                    <Button onClick={this._handlerAddDialog}>
                        <Icon icon="add"></Icon>
                        <Lang lang="add"></Lang>
                    </Button>
                </div>
                <div data-fix="content">
                    <Lock>
                        <Table ref="table" {...this.props} {...this.state.table} />
                    </Lock>
                </div>
            </BoxWidget>
        );
    }


    /**
     *
     * @private
     */
    _handlerAddDialog() {
        this.refs.fittingtype.showAddDialog()
    }

    /**
     *
     * @param edit
     * @private
     */
    _handlerEditDialog(edit) {
        this.setState({ edit }, function () {
            this.refs.fittingtype.showEditDialog({ code: this.state.edit.code, name: this.state.edit.name,id: this.state.edit.id})
        })
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerAddSuccess(data) {
        if (data) {
            this.refs.table.append(data);
     
            
        }
    }


    /**
     *
     * @param update
     * @private
     */
    _handlerEditSuccess(data) {
        if (data) {
            this.refs.table.update(data);
        }
    }






}

export default Enhance(FittingtypeController);
